<?php


namespace App\Http\Controllers\Api;

use App\Constant\ApiV2Constant;

use App\Services\Business\Models\Comment;
use App\Services\Member\Services\UserService;
use App\Services\Member\Interfaces\UserServiceInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Services\Business\Models\MediaVideo;



class MediaVideoController extends BaseController
{


    /**
     * @var UserService
     */
    protected $userService;


    public function __construct(


        UserServiceInterface  $userService

    )
    {
        $this->userService = $userService;

    }


    /**
     * 公开课详情
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function detail($id)
    {

        $video = MediaVideo::query()
            ->where('id', $id)
            ->with(['user','company'])
            ->firstOrFail();
        $isFavorited = false;
        $isFollowed = false;
        $isThumbsuped = false;
        $commentCount = Comment::query()->where('commentable_type',MediaVideo::class)->where('company_id',$video['company_id'])->count();
        $products = ($video->products()->exists()) ? $this->promotionService->getPromotions('mediaVideo',$id) : [];
        if ($this->check()) {

            $userId=$this->id();
            //用户是否被关注
            $isFollowed = $this->userService->isFollowed($userId, $id);
            //是否被收藏
            $isFavorited = $this->userService->isFavorited($userId, $id, MediaVideo::class);
            //是否点赞
            $isThumbsuped = $this->userService->isThumbsuped($userId, $id, MediaVideo::class);
        }

        return $this->successData(compact('video', 'isThumbsuped', 'isFavorited', 'isFollowed', 'products','commentCount'));
    }


    /**
     * 公开课的列表
     * @param Request $request
     * @param $type
     * @param $id
     * @return JsonResponse
     */
    public function comments(Request $request)
    {
        $companyId = $request->input('company_id');
        $size = $request->input('size',20);
        $comments =  Comment::query()->where('commentable_type',MediaVideo::class)->orderByDesc('created_at')->where('company_id',$companyId)->paginate($size);
        $users = $this->userService->getList(array_column($comments->items(), 'user_id'), []);
        $users = arr2_clear($users, ApiV2Constant::MODEL_MEMBER_FIELD);
        $users = array_column($users, null, 'id');

        return $this->data([
            'comments' => $comments,
            'users' => $users,
        ]);
    }






}
